const qiniuUploader = require("../../utils/qiniuUploader");

var appInstance = getApp();
var pageData = {
    data: {
        add1: [true],
        add2: [],
        add3: [true],
        postIm1: [],
        postIm2: [],
        postVio: [],
        disabled: [false, false, false],
        input_value: { valueNull: '' },
        change_ca: {
            BizCatlg: '',
            BizCatlgId: '',
            subCatlg: '',
            subCatlgId: ''
        }
    },
    app_title: '石雕小程序',
    app_description: '加油!',
    page_router: 'page10010',
    page_router1: '10010',
    onLoad: function(e) {
        var that = this;
        var param;
        //var param = e.param;
        if (appInstance.globalData.change_param) {
            param = appInstance.globalData.change_param;
            var add1 = [false, param.company[0].coverImg],
                postIm1 = [param.company[0].coverImg],
                add2 = param.imgUrls,
                postIm2 = [],
                add3 = [false, param.company[0].product_video],
                postVio = [];
            for (var i = 0; i < add2.length; i++) {
                postIm2.push({ img: add2[i] })
            }
            console.log(postIm2);
            for (var i = 1; i < add3.length; i++) {
                postVio.push({ video: add3[i] })
            }
            var cat = param.navItems[0].detail.split('-');
            var input_value = {
                productName: param.company[0].product_name,
                company_name: param.company[0].company_name,
                companyId: param.company[0].companyId,
                prodId: param.company[0].prodId,
                brand: param.navItems[1].detail,
                spec: param.navItems[2].detail,
                unit: param.navItems[3].detail,
                wholesalePrice: param.navItems[4].detail.slice(1),
                salesPrice: param.navItems[4].price.slice(1),
                remarks: param.navItems[5].detail,
                valueNull: ''
            }
            that.setData({
                add1: add1,
                add2: add2,
                add3: add3,
                postIm1: postIm1,
                postIm2: postIm2,
                postVio: postVio,
                disabled: [false, false, false],
                input_value: input_value,
                change_ca: {
                    BizCatlg: cat[0],
                    BizCatlgId: param.navItems[0].url[0],
                    subCatlg: cat[1],
                    subCatlgId: param.navItems[0].url[1]
                }
            })
            appInstance.globalData.change_param = false;
            console.log(param)
        }
        console.log(param);
    },
    onShow: function() {
        var newFound = appInstance.globalData.newFound;
        if (newFound == true) {
            var change_ca = appInstance.globalData.change_ca;
            if (change_ca != '') {
                this.setData({
                    change_ca: change_ca
                })
                appInstance.globalData.change_ca = '';
            }
            return false;
        } else {
            wx.switchTab({
                url: '../page10010/page10010',
                success: function(res) {
                    appInstance.globalData.newFound = !newFound;
                },
            })
        }
    },
    onShareAppMessage: function() {
        var pageRouter = this.page_router;
        var pageRouter1 = this.page_router1;
        return {
            title: this.app_title || '产品设计',
            desc: this.app_description || '',
            path: '/pages/' + pageRouter + '/' + pageRouter1
        }
    },
    didPressChooesImage: function(e) {
        console.log(e)
        var that = this;
        var tapeven = e.currentTarget.id;
        var param;
        wx.showModal({
            title: '提示',
            content: '确认上传？',
            success: function(res) {
                if (res.confirm) {
                    initQiniu();
                    if (tapeven == 'postIm1') {
                        param = that.data.add1.slice(1);
                    } else if (tapeven == 'postIm2') {
                        param = that.data.add2;
                    } else if (tapeven == 'postVio') {
                        param = that.data.add3.slice(1);
                    }
                    appInstance.showToast({ title: '上传中...', icon: 'loading' });
                    for (var i = 0; i < param.length; i++) {
                        qiniuUploader.upload(param[i], (res) => {
                            if (tapeven == 'postIm1') {
                                var postIm1 = that.data.postIm1;
                                postIm1.push(res.imageURL);
                                var disabled = that.data.disabled;
                                disabled[0] = true;
                                that.setData({
                                    postIm1: postIm1,
                                    disabled: disabled
                                });
                            } else if (tapeven == 'postIm2') {
                                var postsrc = { img: res.imageURL }
                                var postIm2 = that.data.postIm2;
                                postIm2.push(postsrc);
                                var disabled = that.data.disabled;
                                disabled[1] = true;
                                that.setData({
                                    postIm2: postIm2,
                                    disabled: disabled
                                });
                            } else if (tapeven == 'postVio') {
                                var postsrc = { video: res.imageURL }
                                var postVio = that.data.postVio;
                                postVio.push(postsrc);
                                var disabled = that.data.disabled;
                                disabled[2] = true;
                                that.setData({ postVio: postVio, disabled: disabled });
                            }
                            console.log(res);
                            appInstance.showToast({ title: '上传成功', icon: 'success' });
                        }, (error) => {
                            console.error('error: ' + JSON.stringify(error));
                        })
                    }
                    // appInstance.showToast({title: '上传成功',icon: 'success'});
                }
            }
        })
    },
    formSubmit: function(e) {
        var that = this;
        //var tapeven = e.currentTarget.id;
        var param = appInstance.globalData.session_key;
        var formparam = e.detail.value;
        var change_ca = that.data.change_ca;
        //console.log(that.data.postIm1[0]);
        var change_ca = that.data.change_ca;
        if (formparam.productName != '' & that.data.postIm1[0] != undefined & change_ca.BizCatlgId != '') {
            wx.showModal({
                title: '提示',
                content: '确认提交？',
                success: function(res) {
                    if (res.confirm) {
                        var postDetail = {
                            source: '1',
                            userId: param.userId,
                            token: param.myXcxSessionId,
                            productName: formparam.productName,
                            bizCatlg: change_ca.BizCatlgId || '',
                            coverImg: that.data.postIm1[0],
                            productId: that.data.input_value.prodId || '',
                            subCatlg: change_ca.subCatlgId || '',
                            companyName: '',
                            companyId: '',
                            productImgs: that.data.postIm2,
                            prodVideos: that.data.postVio,
                            brandId: '',
                            brand: formparam.brand,
                            spec: formparam.spec,
                            wholesalePrice: formparam.wholesalePrice,
                            salesPrice: formparam.salesPrice,
                            qrCode: '',
                            unit: formparam.unit,
                            remarks: formparam.remarks,
                            prodStatus: '',
                        }
                        console.log(postDetail);
                        appInstance.sendRequest({
                                url: '/prod/modifyProd',
                                data: postDetail,
                                method: 'POST',
                                header: {
                                    'content-type': 'application/json'
                                },
                                success: function(res) {
                                    if (res.result == 0) {
                                        var prodId = res.userdata.productId || that.data.input_value.prodId;
                                        wx.navigateTo({
                                            url: '../productdetail/productdetail?isSplot=false&prodId=' + prodId,
                                            success: function(res) {
                                                appInstance.globalData.newFound = false;
                                            },
                                        })
                                        console.log(res.msg)
                                    } else {
                                        console.log(res.msg)
                                            //提示无数据并navigateback
                                    }
                                },
                                fail: function(res) {
                                    console.log('getmode fail');
                                },
                                complete: function(res) {}
                            }, 'https://bc.zhaojx.net/bizcat/webservice/rest')
                            //  appInstance.showToast({title: '提交表单...',icon: 'loading'});
                    }
                },
            })
        } else if (formparam.productName == '') {
            appInstance.showToast({ title: '请输入产品名称！', icon: 'loading' });
        } else if (formparam.BizCatlg == '') {
            appInstance.showToast({ title: '请输入行业分类！', icon: 'loading' });
        } else if (that.data.postIm1[0] == undefined) {
            appInstance.showToast({ title: '请上传封面图片！', icon: 'loading' });
        }
    },
    tapDelete: function(e) {
        var that = this;
        console.log(e);
        var tapeven = e.currentTarget.id;
        wx.showModal({
            title: '提示',
            content: '确认删除？',
            success: function(res) {
                if (res.confirm) {
                    if (tapeven == 'add') {
                        var add = [true];
                        var postIm1 = [];
                        that.setData({ add1: add, postIm1: postIm1 })
                    } else if (tapeven == 'dvideo') {
                        var add3 = [true];
                        var postVio = [];
                        that.setData({ add3: add3, postVio: postVio })
                    } else {
                        var add = that.data.add2;
                        console.log(tapeven)
                        add.splice(tapeven, 1);
                        var postIm2 = that.data.postIm2;
                        postIm2.splice(tapeven, 1);
                        that.setData({ add2: add, postIm2: postIm2 })
                    }
                }
            }
        })
    },
    tapInnerLinkHandler: function(e) {
        var that = this;
        var tapeven = e.currentTarget.id;
        console.log(e);
        if (tapeven == 'add1_c') {
            wx.chooseImage({
                count: 1,
                sizeType: ['original', 'compressed'],
                sourceType: ['camera', 'album'],
                success: function(res) {
                    console.log(res);
                    var tempFilePath = res.tempFilePaths[0];
                    var add = [false, tempFilePath];
                    that.setData({ add1: add })
                },
                fail: function(res) {
                    console.log(res);
                },
                complete: function(res) {
                    console.log(res);
                }
            })
        } else if (tapeven == 'videocamera') {
            wx.chooseVideo({
                sourceType: ['album', 'camera'],
                maxDuration: 60,
                camera: 'back',
                success: function(res) {
                    var add3 = [false, res.tempFilePath];
                    that.setData({ add3: add3 })
                }
            })
        } else {
            wx.chooseImage({
                count: 9,
                sizeType: ['original', 'compressed'],
                sourceType: ['camera', 'album'],
                success: function(res) {
                    var tempFilePaths = res.tempFilePaths;
                    var add = that.data.add2;
                    add.push(tempFilePaths);
                    that.setData({ add2: add })
                        //uploadfile
                }
            })
        }
    },
    EventHandle: function(e) {
        var e = e.currentTarget.id;
        if (e == 'postPro') {

        } else {

        }
    },
    chooseCa: function() {
            wx.navigateTo({
                url: '../pagehyxz/pagehyxz?returnpage=addproduct',
                success: function(res) {
                    // success
                }
            })
        }
        // var tab = JSON.parse(tapeven);
        // console.log(tab.inner_page_link); 
};
Page(pageData);
// 初始化七牛相关参数
function initQiniu() {
    var options = {
        region: 'ECN', // 华东区
        uptokenURL: 'https://bc.zhaojx.net/bizcat/webservice/rest/login/GetQnUpToekn',
        // uptoken: 'xxxx',
        domain: 'http://oh4xneden.bkt.clouddn.com/'
    };
    qiniuUploader.init(options);
}